import dayjs, { type Dayjs, type ManipulateType, type OpUnitType } from 'dayjs';
import 'dayjs/locale/zh-cn';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';
import weekday from 'dayjs/plugin/weekday'; // ES 2015

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.locale('zh-cn');
dayjs.extend(weekday);

export const formatDate = (
  date: number | string | Date | Dayjs = dayjs(),
  formatString = 'YYYY-MM-DD'
) => dayjs(date).format(formatString);

export const today = formatDate();

export const getStartOrEndTimeStamp = (
  dateType: OpUnitType,
  date = dayjs()
) => ({
  start: dayjs(date).startOf(dateType).valueOf(),
  end: dayjs(date).endOf(dateType).valueOf()
});

export const getStartOrEndDate = (dateType: OpUnitType, date = dayjs()) => ({
  start: dayjs(date).startOf(dateType),
  end: dayjs(date).endOf(dateType)
});

export const addDate = (
  date: number | string | Date | Dayjs,
  num: number,
  type: ManipulateType
) => dayjs(date).add(num, type);
